package com.bgs.eat.menuBack.service.impl;

import com.bgs.eat.menuBack.entity.MenuBackEntity;
import com.bgs.eat.menuBack.mapper.MenuBackMapper;
import com.bgs.eat.menuBack.service.MenuBackService;
import com.bgs.eat.roleBack.entity.RoleBackEntity;
import com.bgs.eat.roleBack.service.RoleBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @ClassName MenuBackServiceImpl
 * @Description TODO
 * @Author cjl
 * @Date 2022/10/12 10:20
 **/
@Service
public class MenuBackServiceImpl implements MenuBackService {

    @Autowired(required = false)
    MenuBackMapper menuBackMapper;

    @Autowired(required = false)
    RoleBackService roleBackService;

    /**
    * @Author cjl
    * @Description 递归查询菜单（权限菜单）
    * @Date  15:49
    * @Return java.util.List<com.bgs.eat.menuBack.entity.MenuBackEntity>
    */
    @Override
    public List<MenuBackEntity> listEntityByDG(Integer id) {
        RoleBackEntity roleById = roleBackService.getRoleById(id);
        String[] menuS = roleById.getMenuIds().split(",");
        List<MenuBackEntity> menuList = new ArrayList<>();
        for (int i =0;i<menuS.length;i++){
            Example example = new Example(MenuBackEntity.class);
            example.createCriteria().andEqualTo("id",menuS[i]);
            MenuBackEntity menuBackEntity = menuBackMapper.selectOneByExample(example);
            menuList.add(menuBackEntity);
        }
        for (MenuBackEntity menuOne : menuList) {
            if (menuOne.getParentId() == 0 && menuOne.getDelFlag()==0) {
                //判断是否是一级菜单
                findChildrens(menuOne, menuList);
                //发送给 findChildrens 方法查找子类
            }
        }
        for (int i = menuList.size() - 1; i > 0; i--) {
            if (menuList.get(i).getParentId() != 0 ) {
                //当获取到的不是顶级时
                menuList.remove(i);
                //在List集合中删除此数据
            }
        }
        return menuList;
    }

    /**
    * @Author cjl
    * @Description 递归查询菜单（权限菜单）
    * @Date  15:50
    * @Return void
    * @param menuOne
    * @param menuEntityList
    */
    private static void findChildrens(MenuBackEntity menuOne, List<MenuBackEntity> menuEntityList) {
        // 如果集合Childrens为空
        if (menuOne.getChildrens() == null) {
            // 首次运行时 在menuOne里的childrens创建ArrayList
            menuOne.setChildrens(new ArrayList<MenuBackEntity>());
        }
        for (MenuBackEntity noTop : menuEntityList) {
            if (noTop.getParentId().equals(menuOne.getId()) && noTop.getDelFlag()==0) {
                List<MenuBackEntity> Childrens = menuOne.getChildrens();
                Childrens.add(noTop);
                menuOne.setChildrens(Childrens);
            }
            if (menuOne.getChildrens().size() != 0 && menuOne.getDelFlag()==0) {
                //当传来的参数有子级时,找子类的子类
                List<MenuBackEntity> Childrens = menuOne.getChildrens();
                //获取当前类的所有子类
                for (MenuBackEntity ChildrensOne : Childrens) {
                    findChildrens(ChildrensOne, menuEntityList);
                }
                menuOne.setChildrens(Childrens);
            }
        }
    }


    /**
    * @Author cjl
    * @Description 根据用户权限查询菜单（权限菜单）
    * @Date  15:50
    * @Return java.util.List<com.bgs.eat.menuBack.entity.MenuBackEntity>
    * @param id
    */
    @Override
    public List<MenuBackEntity> listMenuByRoleId(Integer id) {
        RoleBackEntity roleById = roleBackService.getRoleById(id);
        //查询role表中的menuid
        String[] menuS = roleById.getMenuIds().split(",");
        //分割menuids为数组
        List<MenuBackEntity> menus = new ArrayList<>();
        List<MenuBackEntity> listDG = listEntityByDG(id);
        //递归全查
        for (int ib =0;ib<listDG.size();ib++){
            MenuBackEntity menuEntity = new MenuBackEntity();
            MenuBackEntity forDG = listDG.get(ib);
            for (int i =0;i<menuS.length;i++){
                int menuid = Integer.parseInt(menuS[i]);
                if (forDG.getId()==menuid && forDG.getDelFlag()==0){
                    //如果是当前id则添加进来
                    menuEntity = forDG;
                }            }
            if (menuEntity.getId()!=null && menuEntity.getDelFlag()==0){
                //如果不为空时
                menus.add(menuEntity);
                //添加到ArrayList
            }
        }
        return menus;
    }

    /**
    * @Author cjl
    * @Description 树形菜单管理全查
    * @Date  15:51
    * @Return java.util.List<com.bgs.eat.menuBack.entity.MenuBackEntity>
    */
    @Override
    public List<MenuBackEntity> selectMenuBackEntity() {
        List<MenuBackEntity> menuManagerEntities = menuBackMapper.selectMenuBackEntity();
        for (MenuBackEntity menuManagerEntity:menuManagerEntities){
            for (MenuBackEntity menuManagerEntity1:menuManagerEntities){
                if (menuManagerEntity.getId().equals(menuManagerEntity1.getParentId())){
                    List<MenuBackEntity> listChildren = menuManagerEntity.getChildrenList();
                    if (listChildren==null){
                        menuManagerEntity.setChildrenList(new ArrayList<>());
                    }
                    menuManagerEntity.getChildrenList().add(menuManagerEntity1);
                }
            }
        }
        for (int i = menuManagerEntities.size()-1;i>0;i--){
            if (menuManagerEntities.get(i).getParentId()!=0){
                menuManagerEntities.remove(i);
            }
        }
        return menuManagerEntities;
    }

    /**
    * @Author cjl
    * @Description 添加或修改菜单管理
    * @Date  15:52
    * @Return int
    * @param menuBackEntity
    */
    @Override
    public int saveOrUpdateMenuBackEntity(MenuBackEntity menuBackEntity) {
        int i = 0;
        if (menuBackEntity.getId() == 0 ){
            menuBackEntity.setCreateTime(new Date());
            menuBackEntity.setUpdateTime(new Date());
            menuBackEntity.setDelFlag(0);
            i = menuBackMapper.insertSelective(menuBackEntity);
        }else {
            menuBackEntity.setUpdateTime(new Date());
            i = menuBackMapper.updateByPrimaryKeySelective(menuBackEntity);
        }
        return i;
    }
    
    /**
    * @Author cjl
    * @Description 删除菜单管理
    * @Date  15:53
    * @Return java.lang.Integer
    * @param id
    */
    @Override
    public Integer deleteMenuBackEntityById(Integer id) {
        Example example = new Example(MenuBackEntity.class);
        example.createCriteria().andEqualTo("id",id);
        MenuBackEntity menuBackEntity = new MenuBackEntity();
        menuBackEntity.setDelFlag(1);
        int i = menuBackMapper.updateByExampleSelective(menuBackEntity, example);
        return i;
    }

}
